package com.liangtian.mdm.dm.jsp.controller;

import com.alibaba.fastjson.JSON;
import com.liangtian.mdm.base.controller.BaseController;
import com.liangtian.mdm.base.utils.ResultObject;
import com.liangtian.mdm.dm.config.model.AttrConfig;
import com.liangtian.mdm.dm.group.model.AttrGroup;
import com.liangtian.mdm.dm.jsp.common.JspUtil;
import com.liangtian.mdm.dm.jsp.model.GenSelect;
import com.liangtian.mdm.dm.jsp.service.GenJspService;
import com.liangtian.mdm.dm.listener.CacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 复制jsp控制器
 *
 * @author hadoop
 */
@Controller
@RequestMapping("/genJsp")
public class GenJspController extends BaseController {

    @Autowired
    private GenJspService genJspService;

    @Autowired
    private CacheUtil cacheUtil;

    /**
     * * <p>Title: 创建动态jsp页面（参数 modelId）</p>
     *
     * @author : xiaoliu
     * @date :2016年7月28日下午7:42:46
     */
    @ResponseBody
    @RequestMapping(value = "/createJsp", method = RequestMethod.POST)
    public ResultObject createJsp(HttpServletRequest request, String modelId) {
        ResultObject result = null;
        try {
            genJspService.createJsp(modelId);
            result = new ResultObject(1, "创建成功", "创建失败");
//			try {
//			jspService.createDisJsp(modelId);
//			}catch (RuntimeException e){
//				e.printStackTrace();
//			}
        } catch (Exception e) {
            result = new ResultObject(0, "", e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/createJspMore", method = RequestMethod.POST)
    public ResultObject createJspMore(HttpServletRequest request, String modelIds) {
        ResultObject result = null;
        try {
            String[] modelIdArr = modelIds.split(",");
            for (String modelId : modelIdArr) {
                genJspService.createJsp(modelId);
            }
            result = new ResultObject(1, "创建成功", "创建失败");
//			try {
//			jspService.createDisJsp(modelId);
//			}catch (RuntimeException e){
//				e.printStackTrace();
//			}
        } catch (Exception e) {
            result = new ResultObject(0, "", e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 创建分发查询页面
     *
     * @param request
     * @param modelId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/createDisJsp", method = RequestMethod.POST)
    public ResultObject createDisJsp(HttpServletRequest request, String modelId) {
        ResultObject result = null;
        try {
            genJspService.createDisJsp(modelId);
            result = new ResultObject(1, "创建成功", "创建失败");
        } catch (Exception e) {
            result = new ResultObject(0, "", e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * * <p>Title: 创建动态jsp页面（参数 modelId）</p>
     *
     * @author : xiaoliu
     * @date :2016年7月28日下午7:42:46
     */
    @ResponseBody
    @RequestMapping(value = "/checkIsCreateJsp", method = RequestMethod.POST)
    public ResultObject checkIsCreateJsp(HttpServletRequest request, String modelId) {
        ResultObject result = null;
        List<AttrConfig> attrConfig = new ArrayList<AttrConfig>();
//		List<AttrConfig> configLst = new ArrayList<AttrConfig>();
        try {
            List<AttrConfig> attrCon = cacheUtil.getAttrList(modelId);
            for (int i = 0; i < attrCon.size(); i++) {
                AttrConfig con = attrCon.get(i);
                if (1 == con.getIsParentType()) {//如果父子树设置
                    attrConfig.add(con);//添加进去
                }
            }
            /**
             * 默认父子树上下级关系，已经去掉了 2017-6-18 10:56  刘晶去掉此处判断
             */
//			if(attrConfig.size()>0){//如果父子树，上下级关系有多个，那么，就必须设置默认上下级关系
//				for (int i = 0; i <attrConfig.size(); i++) { 
//					AttrConfig config = attrConfig.get(i);
//					if(1==config.getIsDefaultParentType()){//如果设置了默认父子级关系
//						configLst.add(config);
//					}
//				}
//				result = new ResultObject(configLst.size(), "父子树已设置","缺少设置默认父子级关系的设置");
//			}else{
            result = new ResultObject(attrConfig.size(), "父子树已设置", "缺少设置父子级关系的设置");
//			}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * * <p>Title: 根据modelId 和 列名来判断  单选框，复选框，下拉框值得获取</p>
     *
     * @author : xiaoliu
     * @date :2016年7月28日下午7:43:50
     */
    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping(value = "/getSelectData", method = RequestMethod.POST)
    public ResultObject getSelectData(HttpServletRequest request, Map map) {
        List<GenSelect> selectList = null;
        String modelId = request.getParameter("modelId");//模型Id
        String attrColumn = request.getParameter("attrColumn");
        map.put("modelId", modelId);
        map.put("attrColumn", attrColumn);
        String idValue = request.getParameter("id");//模型下一条数据的Id
        String columnValue = request.getParameter("columnValue");
        String isCascad = request.getParameter("isCascad");//判断是不是级联查询操作
        map.put("isCascad", isCascad);
        String searchType = request.getParameter("searchType");
        if (null != idValue && !"".equals(idValue)) {
            map.put("id", idValue);
            map.put("searchType", searchType);
        }
        if (null != columnValue && !"".equals(columnValue)) {
            List<String> list = new ArrayList<String>();
            if (columnValue.startsWith("{") && columnValue.endsWith("}"))
                columnValue = JspUtil.formatSelectCol(columnValue);
            if (columnValue.contains(",")) {
                String[] ss = columnValue.split(",");
                for (int j = 0; j < ss.length; j++) {
                    list.add(ss[j]);
                }
            } else {
                list.add(columnValue);
            }
            map.put("columnValue", list);
        }
        selectList = genJspService.getSelectDataSource(map);
        ResultObject result = new ResultObject(selectList);
        return result;
    }

    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping(value = "/getColum", method = RequestMethod.POST)
    public ResultObject getColum(HttpServletRequest request, Map map) {
        String Id = request.getParameter("Id");
        String modelId = request.getParameter("modelId");
        String attrColumn = request.getParameter("attrColumn");
        map.put("modelId", modelId);
        map.put("attrColumn", attrColumn);
        map.put("Id", Id);
        List<GenSelect> list = genJspService.getColum(map);
        ResultObject result = new ResultObject(list);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/modelView", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String modelView(HttpServletRequest request) {
        String rst = null;
        try {
            String busId = request.getParameter("busiId");
            List<AttrGroup> groupLst = genJspService.queryGroupByModelId(busId);
            rst = JSON.toJSONString(groupLst);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rst;
    }

}
